-------------------------------------------
---------RPG:DM-Player Death Hooks---------
-------------------------------------------
local RPGDM_DeathSounds = {
	Sound("devin/blackbetty/player/male/death1.wav"),
	Sound("devin/blackbetty/player/male/death2.wav"),
	Sound("devin/blackbetty/player/male/death3.wav"),
	Sound("devin/blackbetty/player/male/death4.wav"),
}

function RPGDM_DoPlayerDeath(ply, dmginfo, attacker)
	
	RemoveTalents(ply)
	
	if(ply.Effects != nil)then
		for q, r in pairs(ply.Effects)do
			r.RemoveMe = true
		end
	end
	
	if(attacker:IsPlayer() and ply:EntIndex() != attacker:EntIndex())then
		AddXP(attacker, 100, ply:GetName())
	end
	
	if(ply.MainHand != nil and ply.MainHand:IsValid())then
		ply.MainHand:Remove()
		ply.MainHand = nil
	end
	if(ply.OffHand != nil and ply.OffHand:IsValid())then
		ply.OffHand:Remove()
		ply.OffHand = nil
	end
	if(ply:GetNWEntity("MainWep") != nil and ply:GetNWEntity("MainWep"):IsValid())then
		ply:GetNWEntity("MainWep"):Remove()
		ply:SetNWEntity("MainWep", nil)
	end
	if(ply:GetNWEntity("OffWep") != nil and ply:GetNWEntity("OffWep"):IsValid())then
		ply:GetNWEntity("OffWep"):Remove()
		ply:SetNWEntity("OffWep", nil)
	end
	ply.CanRespawn = false
	if(ply and ply:IsValid())then
		ply.Effects = nil
		if ply:GetRagdollEntity() != nil then
			local oldragdoll = ply:GetRagdollEntity()
			if(ply:Health() <= -25)then
				if(ply.Flag != nil and ply.Flag:IsValid())then
					ply.Flag:Remove()
					ply.Flag = nil
				end
				oldragdoll:Remove()
				if(ply.VisibleItems != nil and table.Count(ply.VisibleItems) > 0)then
					for k, v in pairs(ply.VisibleItems)do
						if (v != nil and v:IsValid()) then
							v:Remove()
						end
					end
					ply.VisibleItems = nil
				end
				local gore = ents.Create("gore")
				gore:SetPos(ply:GetPos() + Vector(0, 0, 40))
				gore:Initialize()
				gore:SetMode("gib")
				ply:SpectateEntity( gore )
				ply:Spectate(OBS_MODE_CHASE)
			else
			
				local ragdoll = ents.Create("prop_ragdoll")
				local Vel = ply:GetVelocity()
				local Col = ply:GetColor()
				local RagColor = Col
				local Corpse = ragdoll
				Corpse:SetModel(ply:GetModel())
				Corpse:SetPos(ply:GetPos())
				Corpse:SetAngles(ply:GetAngles())
				Corpse:SetColor(team.GetColor(ply:Team()))
				Corpse:Spawn()
				Corpse:SetCollisionGroup(COLLISION_GROUP_DISSOLVING)
				Corpse:SetRenderMode( RENDERMODE_TRANSALPHA )
				Corpse:Fire( "alpha", 255, 0 )
				local NumPhysObjects = Corpse:GetPhysicsObjectCount()
				for Bone = 0, NumPhysObjects-1 do
					local PhysObj = Corpse:GetPhysicsObjectNum( Bone )
					if ( PhysObj:IsValid() ) then
						local Pos, Ang = ply:GetBonePosition( Corpse:TranslatePhysBoneToBone( Bone ) )
						PhysObj:SetPos( Pos )
						PhysObj:SetAngles( Ang )
						PhysObj:AddVelocity( Vel )
					end
				end
				oldragdoll:Remove()
				ply:SetPos(Corpse:GetPos())
				ply:SpectateEntity( Corpse )
				ply:Spectate(OBS_MODE_CHASE)
				
				if(ply.Flag != nil and ply.Flag:IsValid())then
					ply.Flag:SetParent(Corpse)
				end
				if(ply.VisibleItems != nil and table.Count(ply.VisibleItems) > 0)then
					for k, v in pairs(ply.VisibleItems)do
						if v:IsPlayer() or !IsValid(v) then return false end
						print("item "..k.."")
						v:SetColor(Color(255, 255, 255, 255))
						v:SetParent(Corpse)
					end
				end
				timer.Simple(RESPAWN_TIME*0.5, function()
					Corpse:EmitSound("devin/blackbetty/weapons/RendTarget.wav", 80, 100+math.random(-10,10))
					ParticleEffect( "player_blood", Corpse:GetPos(), Angle(0,0,0), Corpse )	
					local fadevalue = 255
					local skelefade = 0
					local skeleton = ents.Create("prop_ragdoll")
					local Skeleton = skeleton
						Skeleton:SetModel("models/skeleton/skeleton_whole.mdl")
						Skeleton:SetSkin(2)
						Skeleton:SetParent(Corpse)
						Skeleton:AddEffects( EF_BONEMERGE )
						Skeleton:Spawn()
						Skeleton:Fire( "disablemotion" )
						Skeleton:SetCollisionGroup(COLLISION_GROUP_DISSOLVING)
						Skeleton:SetRenderMode( RENDERMODE_TRANSALPHA )
						Skeleton:Fire( "alpha", 0, 0 )
					timer.Create("corpsefade"..Corpse:EntIndex().."", 0.0015, 255, function()
						fadevalue = fadevalue - 2
						skelefade = skelefade + 2
						if(IsValid(Corpse))then
							Corpse:Fire( "alpha", fadevalue, 0 )
						end
						Skeleton:Fire( "alpha", skelefade, 0 )
					end)
					timer.Create("corpseflies"..Corpse:EntIndex().."", 0.6, 0, function()
						local nextbuzz = math.random(1, 3)
						timer.Simple(nextbuzz, function()
							if(IsValid(Skeleton))then
								Skeleton:EmitSound("ambient/creatures/flies"..math.random(1,5)..".wav", 59, 100+math.random(-10,10))
							end
						end)
					end)
					
					timer.Simple(60, function()
						if(Corpse)then
							Corpse:Remove()
						end
						-- Skeleton:Remove()
						timer.Destroy("corpseflies"..Corpse:EntIndex().."")
					end)
				end)
			end
		end
	end
	
	playerSound = CreateSound(ply, RPGDM_DeathSounds[math.random(1, 4)]);
	playerSound:Play();
	
end

function RPGDM_PlayerDeathThink(ply)
	if(ply.CanRespawn or !ply.CanRespawn and RESPAWN_TIME == 0)then
		ply.CanRespawn = true
	else
		if(!ply.SpawnTimerDisplayed and RESPAWN_TIME > 0)then
			ply.SpawnTimerDisplayed = true
			CreateVisualTimer(RESPAWN_TIME, function()
				ply.CanRespawn = true
				ply.SpawnTimerDisplayed = false
				ply:Spawn()
			end, ""..ply:GetName().." has been slain!", "devin/blackbetty/hudicons/dm.png", ply)
		end
		return false
	end
end

function RemoveFlatline()
	return true
end
hook.Add("PlayerDeathSound", "Remove Flatline Death Sound", RemoveFlatline )
hook.Add("PlayerDeath", "RPGDM Do Player Death", RPGDM_DoPlayerDeath)
hook.Add("PlayerDeathThink", "RPGDM Player Death Think", RPGDM_PlayerDeathThink)


